<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<script th:inline="javascript">
	function getDeptUsers(formCode, userField, deptField, deptCode) {
		// 替换用户列表控件中的名单
		var ajaxData = {
			formCode: formCode,
			userField: userField,
			deptField: deptField,
			deptCode: deptCode
		}
		ajaxPost('/flow/macro/getUserListOptions', ajaxData).then((res) => {
			if (res.code == 200) {
				console.log('res', res);
				setTimeout(function () {
					// 如果可写，才能加入options，则如果不可写，则不能加入
					if (fo(userField).tagName == 'SELECT') {
						var val = $(fo(userField)).val();
						$(fo(userField)).html(res.data.options);
						$(fo(userField)).val(val);
					}
				}, 500);
			}
		});
	}

	function getParentDept(parentDeptField, deptCode) {
		var ajaxData = {
			formCode: formCode,
			deptCode: deptCode
		}
		ajaxPost('/flow/macro/getParentDept', ajaxData).then((res) => {
			console.log('res', res);
			if (res.code == 0) {
				fo(parentDeptField).value = res.data.parentCode;
			}
		});
	}

	function initDept(deptField, userField, formCode, parentDeptField) {
		var deptCode = "";
		if (fo(deptField).tagName=="SELECT") {
			deptCode = $(fo(deptField)).children('option:selected').val();
		} else {
			// 不可写时
			deptCode = fo(deptField).value;
		}
		console.log('deptField', deptField, 'deptCode', deptCode);
		if (userField != '') {
			// 如果用户列表字段可写，才需要获取
			if (fo(userField) && fo(userField).tagName == "SELECT") {
				getDeptUsers(formCode, userField, deptField, deptCode);
			}
		} else if (parentDeptField != '') {
			getParentDept(parentDeptField, deptCode);
		}

		$(fo('[(${deptField})]')).change(function() {
			var deptCode = $(this).children('option:selected').val();
			if (userField != '') {
				if (fo(userField) && fo(userField).tagName == "SELECT") {
					getDeptUsers(formCode, userField, deptField, deptCode);
				} else {
					console.log('部门宏控件[(${deptField})]关联的字段' + userField + ' 不可写');
				}
			} else if (parentDeptField != '') {
				getParentDept('[(${parentDeptField})]', deptCode);
			}
		})
	}
	initDept('[(${deptField})]', '[(${userField})]', '[(${formCode})]', '[(${parentDeptField})]');

	function loadDeptSelectCtl(formCode, fieldName) {
		var thatFieldName = fieldName;
		if (thatFieldName.indexOf("main:") ==0 || thatFieldName.indexOf("other:") == 0) {
			var ary = thatFieldName.split(":");
			thatFieldName = ary[ary.length - 1];
		}
		var ajaxData = {
			op: "loadDeptSelectCtl",
			formCode: formCode,
			fieldName: thatFieldName
		}
		ajaxPost('/flow/macro/loadDeptSelectCtl', ajaxData).then((res) => {
			console.log('macro_js_dept_select res', res);
			if (res.code == 200) {
				$(fo(fieldName)).empty();
				console.log('fo(' + fieldName + ')', fo(fieldName));
				$(fo(fieldName)).append(res.data);
			} else {
				myMsg(res.msg, 'warn');
			}
		});
	}

	var op = '[(${op})]';
	var atUserField[(${#strings.replace(deptField, ':', '_')})] = '[(${atUserField})]';

	if ("forQuery" == op) {
		loadDeptSelectCtl('[(${formCode})]', '[(${deptField})]');
	}

	if (atUserField[(${#strings.replace(deptField, ':', '_')})] != '') {
		// [(${deptField})] 中可能为主表中的部门字段，里面含有:号，而方法名中不能含有:号，会导致报错
		function initAtUserFieldChange() {
			var oldValue_[(${atUserField})] = ""; // 一个不存在的值
			var sint = setInterval(function(){
				if (findObj("[(${atUserField})]")) {
					if (oldValue_[(${atUserField})] != findObj("[(${atUserField})]").value) {
						oldValue_[(${atUserField})] = findObj("[(${atUserField})]").value;
						onAtUserChange('[(${deptField})]');
					}
				} else {
					window.clearInterval(sint);
				}
			},500);

			getCurFormUtil().addInterval(sint, "[(${formName})]");
		}
		initAtUserFieldChange('[(${deptField})]');

		function onAtUserChange(deptField) {
			var ajaxData = {
				formCode: "[(${formCode})]",
				atUser: $(fo('[(${atUserField})]')).val(),
				deptField: deptField
			}
			ajaxPost('/flow/macro/getUserDept', ajaxData).then((res) => {
				console.log('getUserDept', res);
				if (res.code === 200) {
					if (res.data.res === 0) {
						// $(fo("[(${deptField})]")).val(res.data.deptCode);
						$(fo("[(${deptField})]")).select2('val', [res.data.deptCode]);
					}
				}
			});
		}
	}
</script>
</body>
</html>